package com.lazycat.backend.service.impl.address;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.AddressMapper;
import com.lazycat.backend.mapper.UserAddressMapper;
import com.lazycat.backend.pojo.Address;
import com.lazycat.backend.pojo.UserAddress;
import com.lazycat.backend.service.address.AddAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddAddressServiceImpl implements AddAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public Map<String, Object> add(Integer userId, String tag, String address, String contact, String phoneNumber) {
        Address newAddress = new Address(null, tag, address, contact, phoneNumber);
        addressMapper.insert(newAddress);


        List<Address> addresses = addressMapper.selectList(null);

        UserAddress userAddress = new UserAddress(userId, addresses.get(addresses.size() - 1).getAddressId());
        userAddressMapper.insert(userAddress);

        HashMap<String, Object> resp = new HashMap<>();
        resp.put("error_message", "success");
        return resp;

    }
}
